const program = require('commander')
const path = require('path')
const fs = require('fs')
const tpl = require('./template')
const _ = require('lodash')

const filePath = {
  page: '../src/views/',
  comp: '../src/components/'
}

program
  .version('0.1.0')
  .arguments('<type> [name]')
  .action(generate)

program.parse(process.argv)

function generate(type, name) {
  if (!type || !name) {
    console.error('no type or name given!')
    process.exit(1)
  }
  const _path = path.resolve(__dirname, filePath[type])
  for (const key in tpl[type]) {
    let targetName = ''
    if (type === 'comp') {
      targetName = _.camelCase(name)
      targetName = targetName.replace(targetName[0], targetName[0].toUpperCase())
    } else if (type === 'page') {
      targetName = _.kebabCase(name)
    }

    fs.writeFileSync(`${_path}/${name}.${key}`, tpl[type][key]
      .replace(/{{root-class}}/g, _.kebabCase(name))
      .replace(/{{name}}/g, targetName))
    console.log(`created: ${_path}/${name}.${key}`)
  }
  console.log(`\r\n=================== generate ${type} ${name} completed =====================`)
}